package com.ktjy.controller;

import com.alibaba.fastjson2.JSON;
import com.ktjy.pojo.UserDTO;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/mq")
public class ProducerController {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/simple/{message}")
    public String sendSimpleMsg(@PathVariable("message") String message) {
        rabbitTemplate.convertAndSend("", "spring_simple_queue", message);
        return "Simple消息发送成功";
    }

    @GetMapping("/work/{message}")
    public String sendWorkMsg(@PathVariable("message") String message) {
        rabbitTemplate.convertAndSend("", "spring_work_queue", message);
        return "Work消息发送成功";
    }

    /**
     * 发布订阅模式
     */
    @PostMapping("/pub")
    public String sendPub(@RequestBody UserDTO userDTO) {
        // routingKey 为空即可
        rabbitTemplate.convertAndSend("exchange_fanout", "", JSON.toJSONString(userDTO));
        return "消息发送成功";
    }
}
